import { ContactModel } from '~/model/ContactModel'
import { MaterialModel } from '~/model/MaterialModel'
import { PricePolicyModel } from '~/model/PricePolicyModel'
import { contactFromId } from '~/model/utils'

export abstract class PricePolicySerializer {
  static serialize(model: PricePolicyModel): SentData {
    if (!model.consumer) {
      throw new Error(`JobRecordSerializer.serialize: model.consumer => ${model.consumer}`)
    }
    if (!model.product) {
      throw new Error(`JobRecordSerializer.serialize: model.product => ${model.product}`)
    }
    return {
      consumerId: model.consumer.id,
      productId: model.product.id,
      price: model.price,
      note: model.note,
    }
  }

  static async deserialize(data: ReceivedData): Promise<PricePolicyModel> {
    const model = new PricePolicyModel()

    model.id = data.id
    model.consumer = await contactFromId(data.consumerId)
    model.product = await MaterialModel.fromId(data.productId)
    model.price = data.price
    return model
  }
}

interface SentData {
  consumerId: number
  productId: number
  price: number
  note: string
}

interface ReceivedData {
  id: number
  consumerId: number
  productId: number
  price: number
  note: string
}
